const sqlTool = require('../tool/mysql')

const queryTownship = (code) => {
  return new Promise((resolve, reject) => {
    if (!code) {
      reject('编码为空')
      return
    }
    const sql = `select id from township where code = ${code} limit 1;`
    const connection = sqlTool.connection
    connection.query(sql, (err, res) => {
      if (err) {
        console.log(err)
        reject(err)
      }
      resolve(res)
    })
  })
}
const addTownship = (innserVal) => {
  return new Promise((resolve, reject) => {
    if (!innserVal) {
      reject('编码为空')
      return
    }
    const sql = `insert into township (id, code, name, lat, lng, parent_id, level, parent_code) values ('', '${innserVal.code}', '${innserVal.name}', '${innserVal.lat}', '${innserVal.lng}', '${innserVal.parent_id}', '${innserVal.level}', '${innserVal.parent_code}');`
    const connection = sqlTool.connection
    connection.query(sql, (err, res) => {
      if (err) {
        console.log(err)
        reject(err)
      }
      resolve(res)
    })
  })
}

module.exports = {
  queryTownship,
  addTownship
}
